package com.innovation.chengzhangriji.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.ui.HistoryVideoActivity;
import com.innovation.chengzhangriji.ui.model.UIHistoryPhoto;
import com.innovation.chengzhangriji.ui.model.UIHistoryPhotoDate;
import com.innovation.chengzhangriji.ui.model.UIHistoryVideo;
import com.innovation.chengzhangriji.ui.model.UIHistoryVideoContent;
import com.innovation.chengzhangriji.ui.widget.HistoryPhotoDateAdapterViewHolder;
import com.innovation.chengzhangriji.util.ChZhRJUtils;

import java.util.List;

public class HistoryVideoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public interface HistoryVideoAgent {
        HistoryVideoContentHolder.VideoClickListener getVideoClickListener();
        List<UIHistoryPhoto> getItemList();
    }

    private HistoryVideoAgent videoAgent;

    public HistoryVideoAdapter(HistoryVideoAgent videoAgent) {
        this.videoAgent = videoAgent;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        RecyclerView.ViewHolder viewHolder;
        switch (viewType) {
            case UIHistoryPhoto.TYPE_DATE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history_photo_date, parent, false);
                viewHolder = new HistoryPhotoDateAdapterViewHolder(view);
                return viewHolder;
            case UIHistoryPhoto.TYPE_CONTENT:
                view = new RelativeLayout(parent.getContext());
                viewHolder = new HistoryVideoContentHolder(view, videoAgent.getVideoClickListener());
                return viewHolder;

        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case UIHistoryPhoto.TYPE_DATE:
                ((HistoryPhotoDateAdapterViewHolder) holder).setDate(((UIHistoryPhotoDate) videoAgent.getItemList().get(position)).getDate());
                break;
            case UIHistoryPhoto.TYPE_CONTENT:
                UIHistoryVideoContent video = (UIHistoryVideoContent) videoAgent.getItemList().get(position);
                ((HistoryVideoContentHolder) holder).setVideoList(video.getVideos());
                break;
        }
        if(position == getItemCount() - 1) {
            holder.itemView.setPadding(0, 0, 0, (int) ChZhRJUtils.dp2px(10, holder.itemView.getResources().getDisplayMetrics()));
        }
    }

    @Override
    public int getItemCount() {
        return videoAgent.getItemList().size();
    }

    @Override
    public int getItemViewType(int position) {
        return videoAgent.getItemList().get(position).getType();
    }
}
